package com.bl.service.customer.impl;

import java.util.List;

import com.bean.Customer;
import com.bl.action.Command;
import com.bl.action.customer.BeanActions;
import com.bl.service.Service;
import com.bl.service.customer.CustomerFactory;
import com.bl.util.LoggingCommandExecutor;
import com.dao.factory.DaoFactory;
import com.dao.factory.DaoFactory.FactoryType;
import com.dao.factory.StartupFactoryType;

/**
 * @author ${user}
 */
public class CustomerServiceWithLogging
        implements Service<Customer> {

    FactoryType defaultFactoryType = new StartupFactoryType().getDefaultFactoryType();
    DaoFactory daoFactory = DaoFactory.getDaoFactory(defaultFactoryType);
    BeanActions<Customer> customerActions = new BeanActions<Customer>(daoFactory.getCustomerDao(), new CustomerFactory(defaultFactoryType));
    LoggingCommandExecutor commandExecutor = new LoggingCommandExecutor();


    @Override
    public void insertToDbOneByOne(final List<Customer> aCustomers) {
        final Command<Void> insertCustomersToDbOneByOneAction = customerActions.getInsertBeansToDbOneByOneAction(aCustomers);
        commandExecutor.executeCommand(insertCustomersToDbOneByOneAction, "Insert of the generated customers performed in:");
    }


    @Override
    public void insertUsingBatches(final List<Customer> aCustomers) {
        final Command<Void> insertCustomersUsingBatchesAction = customerActions.getInsertBeansUsingBatchesAction(aCustomers);
        commandExecutor.executeCommand(insertCustomersUsingBatchesAction, "Batch Insert of the generated customers performed in: ");
    }


    @Override
    public void deleteFromDbOneByOne(final List<Customer> aCustomers) {
        final Command<Void> deleteCustomersFromDbOneByOneAction = customerActions.getDeleteBeansFromDbOneByOneAction(aCustomers);
        commandExecutor.executeCommand(deleteCustomersFromDbOneByOneAction, "Delete * customers from DB one by one: ");
    }


    @Override
    public void deleteUsingBatches(final List<Customer> aCustomers) {
        final Command<Void> deleteCustomersUsingBatchesAction = customerActions.getDeleteBeansUsingBatchesAction(aCustomers);
        commandExecutor.executeCommand(deleteCustomersUsingBatchesAction, "Batch Delete of the generated customers: ");
    }


    @Override
    public Customer findById(final Integer id) {
        final Command<Customer> findByIdCommand = customerActions.getFindByIdAction(id);
        return commandExecutor.executeCommand(findByIdCommand, "Retrieving customer with id : " + id + " performed in: ");
    }


    @Override
    public List<Customer> generateObjects(final int aNumberOfCustomers) {
        final Command<List<Customer>> generateCustomersAction = customerActions.getGenerateBeansAction(aNumberOfCustomers);
        return commandExecutor.executeCommand(generateCustomersAction, "Generated " + aNumberOfCustomers + " performed in: ");
    }

}
